﻿using Microsoft.SharePoint.ApplicationPages.Calendar.RecurrenceRule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindStyle.SPCalendar.Pattern
{
    public class YearlyByDayPattern : IPattern
    {
        public int Month { get; set; }
        public Ordinal Ordinal { get; set; }
        public DaysOfWeek Days { get; set; }

        public YearlyByDayPattern()
        {
            this.Month = DateTime.Today.Month;
            this.Ordinal = Ordinal.First;
            this.Days = Utility.ParseDayOfWeek(DateTime.Today.DayOfWeek);
        }

        public string ToXML()
        {
            return string.Format("<yearlyByDay yearFrequency=\"1\" {0}=\"TRUE\" weekdayOfMonth=\"{1}\" month=\"{2}\" />",
                    this.Days.ToSPString(),
                    this.Ordinal.ToString().ToLower(),
                    this.Month);
        }


        public void FromRule(RecurrenceRule recurrence)
        {
            this.Month = recurrence.Month;
            this.Ordinal = Utility.ParseOrdinal(recurrence.Ordinal);
            if (recurrence.DaysOfWeek.Any())
                this.Days = Utility.ParseDayOfWeek(recurrence.DaysOfWeek.First());
        }
    }

}
